// pages/classes/index.js
import api from '../../api/api'
import {treeToArray} from '../../utils/util'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    left: '87rpx',
    oneTabList:[],
    twoTabList:[],
    threeTabList: [],
    activeIndex:0,
    showSearch: false,
    searchValue: '',
    searchList: []
  },

  search(e){
    // 点击键盘完成搜索
    this.setData({searchValue: e.detail.value})
    const arr = treeToArray(this.data.oneTabList);

    let arr1 = arr.filter(item => {
      return item.type === 2 && item.name.includes(this.data.searchValue)
    })
    console.log(arr1);
    this.setData({searchList: arr1});
  },

  showSearchTab(){
    this.setData({searchValue: ''});
    this.setData({searchList: []});
    this.setData({showSearch: true});
  },

  closeSearchTab(){
    this.setData({searchValue: ''});
    this.setData({searchList: []});
    this.setData({showSearch: false});
  },

  changeOneTab(e){
    console.log(e.currentTarget.dataset);
    let left = 0
    left = 87 + (e.currentTarget.dataset.index * 184)
    this.setData({left: left + 'rpx'})
    this.setData({twoTabList: 'children' in e.currentTarget.dataset.item ? e.currentTarget.dataset.item.children : []})
    this.setData({threeTabList: 'children' in e.currentTarget.dataset.item.children[0] ? e.currentTarget.dataset.item.children[0].children : []})
  },
  changeTwoTab(e){
    console.log(e);
    this.setData({activeIndex: e.currentTarget.dataset.index})
    this.setData({threeTabList: 'children' in e.currentTarget.dataset.item ? e.currentTarget.dataset.item.children : []})
  },
  changeFourTab(e){
    console.log(e);
    const datalist = getApp().globalData.datalist
    let isHas = false
    for (let i = 0; i < datalist.length; i++) {
      if(datalist[i].id === e.currentTarget.dataset.item.id){
        isHas = true
      }
      break
    }
    if(!isHas){
      getApp().globalData.datalist.push(e.currentTarget.dataset.item)
    }
    
    wx.navigateBack({
      delta: 1
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    api.getClassList({type: 'zy'}).then(res => {
      if(res.data.code === 200){
        this.setData({oneTabList: res.data.data})
        this.setData({twoTabList: 'children' in res.data.data[0] ?  res.data.data[0].children : []})
        this.setData({threeTabList: 'children' in res.data.data[0].children[0] ? res.data.data[0].children[0].children : []})
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({showSearch: false})
    this.setData({searchValue: ''})
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})